<?php
session_start();
//error_reporting(E_ALL ^ E_NOTICE);
require('db.php');

if (isset($_POST['key'])){
    $query = "SELECT (CURRENT_TIMESTAMP - date) as qwe FROM `session` WHERE `uid`='{$_SESSION['id']}'";
    $keydb = mysql_query($query) or die(mysql_error()); 
    while ($row=mysql_fetch_array($keydb)){
        $timer = $row['qwe'];
        if($timer > 7200){
            exit('Old session;');
        }
    }
} else {
    require('class/USERAUTH.php');
    $auth = new USERAUTH();
    $auth->session_status();
}

require('class/CALCULATOR.php');


$calc = new CALCULATOR();
if(isset($_POST['calcme'])){
    $result = $calc->calc($_POST['calcme']);
} else if (isset($_GET['history'])){
    $result = $calc->history();
} else if (isset($_GET['id'])){
    $result = $calc->repeat($_GET['id']);    
} else if(isset($_GET['pro'])){
    foreach($_GET as $key => $val){
        if(strlen($val) > 0){
            switch ($key) {
                case 'sin':
                    $result = $calc->sin($val);
                    break;
                case 'cos':
                    $result = $calc->cos($val);
                    break;
                case 'log':
                    $result = $calc->log10($val);
                    break;
                case 'qad':
                    $result = $calc->sqrt($val);
                    break;
                case 'sqrt':
                    // !!! Два кейса делают одно и то-же. 
                    // !!! Дубликат вылез из CALCULATOR->repeat
                    $result = $calc->sqrt($val);
                    break;
            }
        }
    }
}

echo $result; 

?>
